package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.LifecycleOwner;
import androidx.viewpager.widget.ViewPager;

import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceCustomDBManager;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.DeviceBaseConfigInfo;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceBindingData;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.DeviceState;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindStatusBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.entity.SceneInterruptModeBean;
import com.qiantu.api.entity.SwitchBindActionBean;
import com.qiantu.api.entity.SwitchBindBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.WheelValueBean;
import com.qiantu.phone.event.DeviceKeyNameChangeEvent;
import com.qiantu.phone.ui.dialog.AreaSelectDialog;
import com.qiantu.phone.ui.dialog.BottomInterruptSceneDialog;
import com.qiantu.phone.ui.dialog.BottomProtocolSwitchDialog;
import com.qiantu.phone.ui.dialog.BottomWheelViewDialog;
import com.qiantu.phone.ui.fragment.DeviceListFragment;
import com.qiantu.phone.ui.fragment.SceneListFragment;
import com.qiantu.phone.widget.OperateBindView;
import com.qiantu.phone.widget.ViewPagerIndicator;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AddInterruptSceneActivity  extends AppActivity {
    private TextView selectArea;
    private SceneListFragment sceneListFragment;
    private Map<String,SceneBean> selectSceneMap;
    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, PanelBingingActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }
    private ViewPager viewPager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_interrupt_scene;
    }

    @Override
    protected void initView() {
        getTitleBar().setRightTitle(R.string.save);

        viewPager = findViewById(R.id.view_pager);
        initViewPager();

        selectArea = findViewById(R.id.select_area);
        setOnClickListener(selectArea);
    }

    @Override
    public void onRightClick(View view) {
        Intent data = new Intent();
        List<SceneInterruptModeBean.InterruptSceneBean> beans = new ArrayList<>();
        for(String key : selectSceneMap.keySet()){
            SceneInterruptModeBean.InterruptSceneBean bean = new SceneInterruptModeBean.InterruptSceneBean();
            bean.setSceneSerialNo(key);
            bean.setSubSceneSerialNo(selectSceneMap.get(key).getSceneSerialNo());
            SceneBean sceneBean = (SceneBean) selectSceneMap.get(key).getTemporary();
            bean.setSceneName(sceneBean.getName());
            bean.setSubSceneName(selectSceneMap.get(key).getName());
            beans.add(bean);
        }

        data.putExtra("selectSceneData", (Serializable) beans);
        setResult(RESULT_OK, data);
        finish();
    }

    private void initViewPager() {
        List<Fragment> fragments = new ArrayList<>();
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return 1;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return null;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                super.destroyItem(container, position, object);
            }
        });

        sceneListFragment = SceneListFragment.newInstance(SceneListFragment.TYPE_INTERRUPT_LIST,3);
        List<SceneInterruptModeBean.InterruptSceneBean> rules = getSerializable("rules");
        sceneListFragment.setRules(rules);
        sceneListFragment.setActivity(this);
        fragments.add(sceneListFragment);
        viewPager.setCurrentItem(0);
    }

    @Override
    protected void initData() {
        //获取通知消息
        selectSceneMap = new HashMap<>();
        selectArea.setText(getString(R.string.unlimited_area) );
        List<SceneBean> scenes = LLSceneDBManager.getInstance(getContext()).getAll();
        sceneListFragment.setTotalData(scenes);
    }
    @Override
    public void onClick(View view) {
        if (view == selectArea) {
            showSelectAreaDialog(selectArea);
        }
    }

    AreaSelectDialog selectDialog;

    private void showSelectAreaDialog(View atView) {
        if (selectDialog == null) {
            selectDialog = new AreaSelectDialog(getContext(), AreaSelectDialog.TYPE_SCENE) {
                @Override
                public void onSave(List<SceneBean> result) {
                    sceneListFragment.setTotalData(result);
//                    selectDialog.setTitle(sceneAdapter.getItemCount());
                    selectArea.setText(selectDialog.getTitle());
                }
            };
            new XPopup.Builder(getContext())
                    .dismissOnBackPressed(true)
                    .atView(atView)
                    .offsetX(DensityUtils.dip2px(getContext(), 0))
                    .offsetY(-DensityUtils.dip2px(getContext(), 50))
                    .asCustom(selectDialog);
        }
        List<FloorBean> floors = LLFloorDBManager.getInstance(getContext()).getNotEmptyRoomFloors();
        floors.remove(0);
        selectDialog.setFloorBeans(floors);
        selectDialog.show();
    }

    private BottomInterruptSceneDialog mBottomProtocolSwitchDialog;
    public void showProtocolDialog(SceneBean sceneBean) {
        if (mBottomProtocolSwitchDialog == null) {
            mBottomProtocolSwitchDialog = new BottomInterruptSceneDialog(this);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomProtocolSwitchDialog);
        }
        mBottomProtocolSwitchDialog.setScene(sceneBean);
        if(selectSceneMap.get(sceneBean.getSceneSerialNo()) != null){
            mBottomProtocolSwitchDialog.setSelected(selectSceneMap.get(sceneBean.getSceneSerialNo()));
        }
        mBottomProtocolSwitchDialog.setDialogClickListener(new BottomInterruptSceneDialog.DialogClickListener() {
            @Override
            public void onConfirmClick(SceneBean subScene) {
                mBottomProtocolSwitchDialog.destroy();
                subScene.setTemporary(sceneBean);
                selectSceneMap.put(sceneBean.getSceneSerialNo(),subScene);
                sceneListFragment.addData(sceneBean);
            }
        });
        mBottomProtocolSwitchDialog.show();
    }

    public void removeSelect(String sceneSerialNo){
        selectSceneMap.remove(sceneSerialNo);
    }
}